package org.shock.ss.sms.action.app;

import java.util.List;
import java.util.Map;

import org.shock.ss.sms.action.BaseAction;
import org.shock.ss.sms.bean.app.CustomerInfo;
import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.commons.utils.Util;
import org.shock.ss.sms.dao.app.impl.CustomerDaoImpl;
import org.shock.ss.sms.service.app.CustomerService;
import org.shock.ss.sms.service.app.impl.CustomerServiceImpl;

import com.alibaba.fastjson.JSON;

public class CustomerAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private CustomerService cs = new CustomerServiceImpl((CustomerDaoImpl) DaoFactory.getDao("CustomerDao"));
	private List customerList;
	private CustomerInfo customer = new CustomerInfo();
	private String ids;
	private String query;

	public String customerAdmin() throws Exception {
		return SUCCESS;
	}

	public String listCustomer() throws Exception {

		if (!Util.isEmpty(customer.getName())) {
			sqladd.append(" and  name like '%" + customer.getName() + "%'");
		}
		if (!Util.isEmpty(customer.getPhone())) {
			sqladd.append(" and phone like '%" + customer.getPhone() + "%'");
		}
		if (pageno == 1) {
			pageno = (start / rownum) + 1;
		}
		String sql = "select * from " + CustomerInfo.getTable() + " where 1=1" + sqladd;
		String sqlCount = "select count(*) from " + CustomerInfo.getTable() + " where 1=1" + sqladd;
		pager = cs.list(dir, sort, pageno, rownum, sqlCount, sql);
		totalCount = pager.getTotalCount();
		customerList = pager.getList();
		jsonString = JSON.toJSONString(pager);
		System.out.println(jsonString);
		return SUCCESS;
	}

	public String viewCustomer() throws Exception {
		Map map = cs.view("select * from " + customer.getTable() + " where id=" + customer.getId());
		jsonString = JSON.toJSONString(map);
		System.out.println(jsonString);
		return SUCCESS;
	}

	@RightAnno(rightValue = "delCustomer", rightNote = "删除客户")
	public String delCustomer() throws Exception {
		op = "ajax";
		cs.del("delete from " + customer.getTable() + " where id in (" + ids + ")");
		success = true;
		message = "删除成功!";
		jsonString = "{success:" + success + ",message:'" + message + "'}";
		return SUCCESS;
	}

	@RightAnno(rightValue = "saveCustomer", rightNote = "保存客户")
	public String saveCustomer() throws Exception {
		if (null == customer.getId()) {
			return addCustomer();
		} else {
			return updateCustomer();
		}
	}

	public String updateCustomer() throws Exception {
		cs.update(customer);
		jsonString = "{success:true,message:'保存成功',customerid:'" + customer.getId() + "'}";
		return SUCCESS;
	}

	public String addCustomer() throws Exception {
		Long customerid = ((CustomerInfo) cs.addBean(customer)).getId();
		jsonString = "{success:true,message:'保存成功',customerid:'" + customerid + "'}";
		return SUCCESS;
	}

	public List getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List customerList) {
		this.customerList = customerList;
	}

	public CustomerInfo getCustomer() {
		return customer;
	}

	public void setCustomer(CustomerInfo customer) {
		this.customer = customer;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

}
